package cn.javacc.nav.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import cn.javacc.core.database.BaseDao;
import cn.javacc.nav.domain.Category;
import cn.javacc.util.Status;

/**
 * @author mzh2008@163.com 2012-11-11 上午8:42:21
 * 
 */
@Repository
public class CategoryDao extends BaseDao<Category> {

	public CategoryDao() {
		super(Category.class);
	}

	@SuppressWarnings("unchecked")
	public List<Category> findAll(int offset, int fetchSize) {
		Query query = entityManager.createQuery("from Category where status > 0");
		query.setFirstResult(offset);
		query.setMaxResults(fetchSize);
		return query.getResultList();
	}

	public int getTotalSize() {
		Query query = entityManager.createQuery("select count(1) from  Category where status > 0");
		return ((Long) query.getSingleResult()).intValue();
	}

	public void delete(Long id) {
		entityManager.createQuery("update Category set status=?1,lastUpdateTime=?2 where id=?3").setParameter(1, Status.DELETE).setParameter(2, new Date()).setParameter(3, id).executeUpdate();
	}

}
